using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace KeyPress
{
    class KeyPressGame
    {
        // Stored key-press lists to compare against.
       
       static Keys[] m_aKeysHeldDown;
       static List<Keys> m_lKeysPressedThisFrame = new List<Keys>();
       static List<Keys> m_lKeysReleasedThisFrame = new List<Keys>();

        public KeyPressGame()
        {
            // Set the initial key state.
            m_aKeysHeldDown = Keyboard.GetState().GetPressedKeys();
           
        }
	    #region Properties
	    static public Keys[] KeysHeldDown
	    {
	        get{return  m_aKeysHeldDown;}
	        set{ m_aKeysHeldDown = value;}
	    }
	    static public List<Keys> KeysPressed
	    {
	        get{return m_lKeysPressedThisFrame;}
	        set{m_lKeysPressedThisFrame = value;}
	    }
	    static public List<Keys> KeysReleased
	    {
	        get{return m_lKeysReleasedThisFrame;}
	        set{m_lKeysReleasedThisFrame = value;}
	    }
	    #endregion

        static public bool KeyPressedThisFrame(Keys key)
        {
            if (m_lKeysPressedThisFrame.Contains(key))
            {
                return true;
            }
            return false;
        }
        public bool KeyReleasedThisFrame(Keys key)
        {
            if (m_lKeysReleasedThisFrame.Contains(key))
            {
                return true;
            }
            return false;
        }
        Keys[] KeysHeldDownThisFrame()
        {
            return m_aKeysHeldDown;
        }
        static public void UpdateInput(float elapsed)
        {
            // Clear our pressed and released lists.
            m_lKeysPressedThisFrame.Clear();
            m_lKeysReleasedThisFrame.Clear();

            // Interpret pressed key data between arrays to
            // figure out just-pressed and just-released keys.
            KeyboardState currentState = Keyboard.GetState();
            Keys[] currentKeys = currentState.GetPressedKeys();

            // First loop, looking for keys just pressed.
            for (int currentKey = 0; currentKey < currentKeys.Length; currentKey++)
            {
                bool found = false;
                for (int previousKey = 0; previousKey < m_aKeysHeldDown.Length; previousKey++)
                {
                    if (currentKeys[currentKey] == m_aKeysHeldDown[previousKey])
                    {
                        // The key was pressed both this frame and last; ignore.
                        //found = true;
                        break;
                    }
                }
                if (!found)
                {
                    // The key was pressed this frame, but not last frame; it was just pressed.
                    m_lKeysPressedThisFrame.Add(currentKeys[currentKey]);
                }
            }

            // Second loop, looking for keys just released.
            for (int previousKey = 0; previousKey < m_aKeysHeldDown.Length; previousKey++)
            {
                bool found = false;
                for (int currentKey = 0; currentKey < currentKeys.Length; currentKey++)
                {
                    if (m_aKeysHeldDown[previousKey] == currentKeys[currentKey])
                    {
                        // The key was pressed both this frame and last; ignore.
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    // The key was pressed last frame, but not this frame; it was just released.
                    m_lKeysReleasedThisFrame.Add(m_aKeysHeldDown[previousKey]);
                }
            }

            // Set the held state to the current state.
            m_aKeysHeldDown = currentKeys;
            
        }

        

    }
}